package com.zzzzzz.content.pictext;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zzzzzz.common.GlobalConstants;
import com.zzzzzz.content.tag.TagService;
import com.zzzzzz.core.exception.NoSuchModelException;

@Service("PicTextService")
public class PicTextServiceImpl implements PicTextService {
	
	@Resource
	private PicTextDao picTextDao;
	@Resource
	private TagService tagService;
	
	@Transactional
	public Long add(Long userId, PicTextForm picTextForm) {
		PicText picText = new PicText();
		picTextForm.updatePicText(picText);
		picText.setUserId(userId);
		picText.setLooks(0);
		picText.setLikes(0);
		picText.setReplies(0);
		picText.setStatus(0);
		picTextDao.add(picText);
		
		Long id = picText.getId();
		tagService.updateTagByAddTopic(id, picTextForm.getTags());
		
		return id;
	}
	
	
	@Transactional
	public void del(Long id, Long userId) throws NoSuchModelException {
		PicText picText = picTextDao.findById(id);
		picTextDao.editStatus(id, GlobalConstants.STATUS.DELETED, userId);
		tagService.updateTagByDelTopic(id, picText.getTags());
	}
	
	
	@Transactional
	public void edit(Long userId, Long id, PicTextForm picTextForm) {
		PicText picText = new PicText();
		picText.setId(id);
		picTextForm.updatePicText(picText);
		picText.setUserId(userId);
		
		picTextDao.edit(picText);
		tagService.updateTagByEditTopic(id, picTextForm.getTags());
	}
	
	
	public void editPicIds(Long id, String picIds, Long userId) {
		picTextDao.editPicIds(id, picIds, userId);
	}
	
	public void plusLooks(Long id) {
		picTextDao.editLooks(id, 1);
	}

	public void plusReplies(Long id, Long userId) {
		picTextDao.editReplies(id, 1);
	}
	
	public void editLikes(Long id, Integer likesPlus) {
		picTextDao.editLikes(id, likesPlus);
	}
	
	public List<PicTextListView> find(Long userId, String orderByField, int start, int offset){
		return picTextDao.findByPicTextId(userId, orderByField, start, offset);
	}
	
	public long findByPicTextIdCount(Long userId){
		return picTextDao.findByPicTextIdCount(userId);
	}
	
}
